﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ProjectSunny.Admin.Controls
{
    public partial class EditProduct : System.Web.UI.UserControl
    {
        public string image;
        public string name;
        public string discription;
   

        public string summary;
        public string cate1_id;
        public string cate2_id;
        public string cate3_id;

        public string cate1_name;
        public string bigimage;
      
        public int idproduct;

        protected void Page_Load(object sender, EventArgs e)
        {
            idproduct = Convert.ToInt32(Request.QueryString["idproduct"]);
            DataTable dt = DAL.DCollector.DBCollector.Instance.StoredSelectTableWhereID("tbl_ThietKeWebsite","ThietKeWebsite_ID", idproduct.ToString());
            cate1_id = dt.Rows[0]["Category_ID1"].ToString();
            cate2_id = dt.Rows[0]["Category_ID2"].ToString();
            cate3_id = dt.Rows[0]["Category_ID3"].ToString();

            image = dt.Rows[0]["ThietKeWebsite_Image"].ToString();
            name = dt.Rows[0]["ThietKeWebsite_Name"].ToString();
            discription = dt.Rows[0]["ThietKeWebsite_Description"].ToString();

            summary = dt.Rows[0]["ThietKeWebsite_Summary"].ToString();
            txtProductName.Text = name;

            CKEditorMota.Text = summary;
            CKEditorChitiet.Text = discription;



            if (!Page.IsPostBack)
            {
                dt = DAL.DCollector.DBCollector.Instance.StoredSelectTableWhereID("tbl_Category","Category_ID2", "0");
                ListItem lt1 = new ListItem("Chon danh muc", "-1");
                ddlCategory1.Items.Add(lt1);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    lt1 = new ListItem(dt.Rows[i]["Category_Name"].ToString(), dt.Rows[i]["Category_ID1"].ToString());
                    ddlCategory1.Items.Add(lt1);
                }
                for (int i = 0; i < ddlCategory1.Items.Count; i++)
                {
                    if (ddlCategory1.Items[i].Value == cate1_id)
                    {
                        ddlCategory1.Items[i].Selected = true;
                    }
                }
                //Load du lieu
                dt = DAL.DCollector.DBCollector.Instance.StoredSelectTableWhereID("tbl_Category","Category_ID2", cate1_id.ToString());
                ListItem lt2 = new ListItem("Chon danh muc", "-1");
                ddlCategory2.Items.Add(lt2);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    lt2 = new ListItem(dt.Rows[i]["Category_Name"].ToString(), dt.Rows[i]["Category_ID1"].ToString());
                    ddlCategory2.Items.Add(lt2);
                }
                for (int i = 0; i < ddlCategory2.Items.Count; i++)
                {
                    if (ddlCategory2.Items[i].Value == cate2_id)
                    {
                        ddlCategory2.Items[i].Selected = true;

                    }
                }
                dt = DAL.DCollector.DBCollector.Instance.StoredSelectTableWhereID("tbl_Category","Category_ID2", cate2_id.ToString());
                ListItem lt3 = new ListItem("Chon danh muc", "-1");
                ddlCategory3.Items.Add(lt3);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    lt3 = new ListItem(dt.Rows[i]["Category_Name"].ToString(), dt.Rows[i]["Category_ID1"].ToString());
                    ddlCategory3.Items.Add(lt3);
                }
                for (int i = 0; i < ddlCategory3.Items.Count; i++)
                {
                    if (ddlCategory3.Items[i].Value == cate3_id)
                    {
                        ddlCategory3.Items[i].Selected = true;

                    }
                }
             

     
            }
        }


        protected void btnUpdateThietKeWebsite_Click(object sender, EventArgs e)
        {
           
            int idproduct = Convert.ToInt32(Request.QueryString["idproduct"]);
            DataTable dt = DAL.DCollector.DBCollector.Instance.StoredSelectTableWhereID("tbl_ThietKeWebsite","ThietKeWebsite_ID", idproduct.ToString());
            bigimage = dt.Rows[0]["ThietKeWebsite_Image"].ToString();
        
            cate1_name = ddlCategory1.SelectedItem.ToString();


         
            string productname = txtProductName.Text;
            string summary = CKEditorMota.Text;
            string description = CKEditorChitiet.Text;
     

            if (productname.Equals(""))
            {
                Response.Write("<script>alert('Vui lòng nhập tên sản phẩm...')</script>");
            }
            else
            {
               
               
              
                        int cate1_index = ddlCategory1.SelectedIndex;

                        int cate2_index = ddlCategory2.SelectedIndex;
                        int cate3_index = ddlCategory3.SelectedIndex;

                        if (cate1_index > 0)
                        {
                            int cate1_id = Convert.ToInt32(ddlCategory1.SelectedValue.ToString());

                            int cate2_id = 0;
                            int cate3_id = 0;


                            if (cate2_index < 1)
                            {
                                cate2_id = cate1_id;
                            }
                            else
                            {
                                cate2_id = Convert.ToInt32(ddlCategory2.SelectedValue.ToString());
                            }
                            if (cate3_index < 1)
                            {
                                cate3_id = cate2_id;
                            }
                            else { cate3_id = Convert.ToInt32(ddlCategory3.SelectedValue.ToString()); }


                            if (LIB.Validate.CheckFileType(fulImage.FileName))
                            {
                                LIB.Validate saveimage = new LIB.Validate();

                           

                                if (fulImage.HasFile)
                                {
                                    File.Delete(MapPath(bigimage));
                                    saveimage.ThayDoiKichThuocAnh(Server.MapPath("/Resources/Images/Image-Products/" + cate1_name + "/"), idproduct + "a" + Path.GetExtension(fulImage.FileName), 800, fulImage.PostedFile.InputStream);
                                }
                               
                              
                                DAL.DHelpers.Column[] cl = new DAL.DHelpers.Column[6];
                                cl[0] = new DAL.DHelpers.Column("Category_ID1", cate1_id.ToString(), DAL.DHelpers.DataType.DataTypeSql.Varchar);
                                cl[1] = new DAL.DHelpers.Column("Category_ID2", cate2_id.ToString(), DAL.DHelpers.DataType.DataTypeSql.Varchar);
                                cl[2] = new DAL.DHelpers.Column("Category_ID3", cate3_id.ToString(), DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                              
                                cl[3] = new DAL.DHelpers.Column("ThietKeWebsite_Name", productname, DAL.DHelpers.DataType.DataTypeSql.Varchar);

                                cl[4] = new DAL.DHelpers.Column("ThietKeWebsite_Summary", summary, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                                cl[5] = new DAL.DHelpers.Column("ThietKeWebsite_Description", description, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                             
                                string colWhere =  "ThietKeWebsite_ID";

                               string colValue =  idproduct.ToString();

                                DAL.DCollector.DBCollector.Instance.StoredUpdate("tbl_ThietKeWebsite", cl, colWhere, colValue);
                                Response.Write("<script>alert('Cập nhật thành công...')</script>");

                            }
                            else
                            {
                                Response.Write("<script>alert('Chỉ có thể upload file(.jpg,.png,.jpeg)')</script>");
                            }


                        }

                


            }
        }

        protected void Cate1_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Chon danh muc cap 1 load danh muc cap 2
            ddlCategory2.Items.Clear();
            ddlCategory3.Items.Clear();

            int index = ddlCategory1.SelectedIndex;
            if (index > 0)
            {
                int id = Convert.ToInt32(ddlCategory1.SelectedValue);
                // Load du lieu
                DataTable dt = DAL.DCollector.DBCollector.Instance.StoredSelectTableWhereID("tbl_Category","Category_ID2", id.ToString());
                ListItem lt = new ListItem("Chon danh muc", "-1");
                ddlCategory2.Items.Add(lt);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    lt = new ListItem(dt.Rows[i]["Category_Name"].ToString(), dt.Rows[i]["Category_ID1"].ToString());
                    ddlCategory2.Items.Add(lt);
                }

            }
        }

        protected void Cate2_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Chon danh muc cap 1 load danh muc cap 2
            ddlCategory3.Items.Clear();
            int index = ddlCategory2.SelectedIndex;
            if (index > 0)
            {
                int id = Convert.ToInt32(ddlCategory2.SelectedValue);
                // Load du lieu
                DataTable dt = DAL.DCollector.DBCollector.Instance.StoredSelectTableWhereID("tbl_Category","Category_ID2", id.ToString());
                ListItem lt = new ListItem("Chon danh muc", "-1");
                ddlCategory3.Items.Add(lt);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    lt = new ListItem(dt.Rows[i]["Category_Name"].ToString(), dt.Rows[i]["Category_ID1"].ToString());
                    ddlCategory3.Items.Add(lt);
                }

            }
        }


    }
}